package com.kidedu.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class KeyWordFilter {

	//"http://www.jb51.net/article/58734.htm"
	public static boolean doFilter(String url, String str) {
		try{
            URL u = new URL(url);
            URLConnection connection = u.openConnection();
            HttpURLConnection htCon = (HttpURLConnection) connection;
            int code = htCon.getResponseCode();
            if (code == HttpURLConnection.HTTP_OK)
            {
                System.out.println("find the website");
                BufferedReader in = new BufferedReader(new InputStreamReader(htCon.getInputStream()));
                String inputLine;
                while ((inputLine = in.readLine()) != null){
                	System.out.println(inputLine);
                	if(inputLine.indexOf(str) > -1){
                		in.close();
                		return true;
                	}
                }
                in.close();
            }
            else
            {
                System.out.println("Can not access the website");
            }
        }
        catch(MalformedURLException e )
        { 
            System.out.println("Wrong URL");
        }
        catch(IOException e)
        {
            System.out.println("Can not connect");
        }
		return false;
	}

	public static void main(String[] args) {
		System.out.println(doFilter("http://www.jb51.net/article/58734.htm", "hhaha"));;
	}
}
